<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<meta name="viewport" content="maximum-scale=1.0,minimum-scale=1.0,user-scalable=0,width=device-width,initial-scale=1.0"/>
		<meta name="format-detection" content="telephone=no"/>
		<title>故障定位</title>
		<style>
			.dailog_box{
				width: 80%;
				background-color: #fff;
				position: absolute;
				left: 50%;
				margin-left: -40%;
				top: 20%;
				border-radius: 4px;
				overflow: hidden;
				font-size: 14px;
			}
			.inputstyle{
				background-color:#FAFAFA;
				line-height: 16px;
				padding: 4px 6px;
				border:#c5c5c5 1px solid;
				border-radius: 3px;
				width: 94%;
			}
			.tit_box{
				height: 40px;
				text-align: center;
				background-color: #DB4537;
				color: #fff;
				line-height: 40px;
				font-size: 16px;
			}
			.button2{
				width: 45%;
				float: left;
				text-align: center;
				line-height: 40px;
				font-size: 14px;
				background-color: #46A3FF;
				color: #fff;
			}
			.button{
				width: 45%;
				float: right;
				text-align: center;
				line-height: 40px;
				font-size: 14px;
				background-color: #d0d0d0;
				color: #fff;
			}
			.cardButtonDiv{
				overflow: hidden;
				margin-bottom: 10px;
			}
			.cardDiv{
				margin-bottom: 10px;
			}
		</style>
	</head>
	<body>
		<div class="dailog_box">
			<div class="tit_box">
				签   到
			</div>
			<div style="padding:4px 10px;">
				<div id="sddDivId" >
					<div  class="cardDiv" style="margin-top: 10px; " >
						<div id="user_name">
							签到人
						</div>
					</div>
					<div id="telId" class="cardDiv">
						<div class="cardLabel">
							联系电话：
						</div>
						<div class="cardCon">
							<input id="contactTelTextId" class="inputstyle" type="text">
						</div>
					</div>
					<div id="locaId" class="cardDiv">
						<div class="cardTextareaLabel">
							所在位置：
						</div>
						<div class="cardTextareaCon">
							<textarea id="locationId" class="inputstyle" rows="8" readonly="readonly"></textarea>
						</div>
					</div>
					<div class="cardDiv" id="err_cause" style="display:none">
						<div class="cardTextareaLabel">
							偏差说明：
						</div>
						<div class="cardTextareaCon">
							<textarea id="otherDealInfoId" rows="4"  style="overflow:auto;"></textarea>
						</div>
					</div>
				</div>
					<div class="cardButtonDiv">
						<div class="button2"  onclick="signOptFunc();">
							确定
						</div>
						<div class="button"  onclick="closeFre()">
							取消
						</div>
					</div>
			</div>

		</div>
	</body>
	<script src="../../script/jquery-1.11.3.min.js"></script>
	<script type="text/javascript" src="../../script/api.js"></script>
	<script type="text/javascript" src="../../script/doT.min.js"></script>
	<script type="text/javascript" src="../../script/common.js"></script>
	<script type="text/javascript" src="../../script/config.js"></script>
	<script type="text/javascript" src="../../script/client.api.js"></script>
	<script type="text/javascript" src="../../script/aui-alert.js"></script>
	<script type="text/javascript" src="../../script/debug.js"></script>
	<script type="text/javascript">
		var idclean = "";
		var type = "";
		var distance;
		var isSuper = "N";
		var lon;
		var lat;
		var stLon;
		var stLat;
		apiready = function() {
			// api.addEventListener({
			// 	name : 'viewappear'
			// }, function(ret, err) {
			// 	api.closeFrame({
			// 		name : 'billMenu'
			// 	});
			// });
			restartLocationFunc();
			initRelief()
		};

		function closeFre(){
				api.closeWin();
		}

		//初始化故障定位信息
		function initRelief() {
			var user = $api.getStorage('user');
			var generationBillMenu = $api.getStorage('generationBillMenu');
			console.log(getObj(generationBillMenu));
			$api.byId('user_name').innerHTML = "操作人：" + user.username;
			$api.byId('contactTelTextId').value = user.mobilephone;

			var location = $api.getStorage('LAST_GPS_LOCATION1');
			var lon1 = $api.getStorage("lon1");
			var lat1 = $api.getStorage("lat1");
			lon = location.lon != null ? location.lon : lon1;
			lat = location.lat != null ? location.lat : lat1;

			 stLon = generationBillMenu.baidux||null;
			 stLat = generationBillMenu.baiduy||null;

			lon = displaynum(lon);
			lat = displaynum(lat);
			var info = "";
			distance = GetDistance(stLat, stLon, lat, lon);


				if (distance <= 100) {
					info = "站址经度是：" + stLon + "\n站址纬度是：" + stLat + "\n当前经度是：" + lon + "\n当前纬度是：" + lat + "\n当前时间：" + location.time + "\n偏差：" + distance + "米" + "\n情况描述：偏差在100米范围内，可以进行手机定位";
					isSuper = "N";
				} else if (distance > 100) {
					console.log("时间：" + location.timestamp);
					info = "站址经度是：" + stLon + "\n站址纬度是：" + stLat + "\n当前经度是：" + lon + "\n当前纬度是：" + lat + "\n当前时间：" + location.time + "\n偏    差：" + distance + "米" + "\n情况描述：偏差已经超过100米，请在空旷的地方进行重新定位或者填写误差过大原因";
					isSuper = "Y";
				}

			$api.byId('locationId').innerHTML = info;

		}

		function signOptFunc() {
				var generationBillMenu = $api.getStorage('generationBillMenu');
				var location = $api.getStorage('LAST_GPS_LOCATION1');
				var user = $api.getStorage('user');

				var userId = user.userid;

				var linkInfo = $api.byId('contactTelTextId').value;
				var remark = $api.byId('locationId').value;
				var causeDealInfo = $api.byId('otherDealInfoId').value;

				if (causeDealInfo == "" &&( type == "UPSTATIONSIGN"||type == "XUNJIAN") && isSuper == "Y") {
					api.toast({
						msg : "偏差说明必填",
						duration : 1000,
						location : 'middle'
					});
					return;
				}

				var opts={
					id : generationBillMenu.id,
					linkInfo : linkInfo,
					signdate:(new Date()).pattern("yyyy-MM-dd HH:mm:ss"),
					signtime:(new Date()).pattern("yyyy-MM-dd HH:mm:ss"),
					remark : remark,
					rangesite:distance,
					cause : causeDealInfo,
				};

				api.showProgress({
					title : '处理中',
					modal : true
				});
				console.log(getObj(opts));
				$client.upStationSignGeneration(opts,function(ret, err) {
					console.log(getObj(ret));
					if (ret) {
						api.toast({
							msg : ret.errorMsg,
							location : 'middle'
						});
						window.setTimeout(function() {
							api.closeFrame();
						}, 1100);
					} else {
						api.toast({
							msg : '接口访问失败',
							location : 'middle'
						});
					}
					api.hideProgress();
				});

		}

		function restartLocationFunc() {
			if(api.pageParam.online!=8){
				api.showProgress({
					title : '定位中……',
					modal : true
				});
			}

			getHeartFromGps("upStationSign_win", "upStationSign_frm");
		}

		function getHeartFromGps(winName, frmName) {
			var script = "startLocation('" + winName + "','" + frmName + "');";
			console.log("异步加载" + script);
			api.execScript({
				name : "home_win",
				frameName : 'gps',
				script : script
			});
			     gps_invoke();
		}

		function gps_invoke() {
			var lastLocation = $api.getStorage('LAST_GPS_LOCATION1');
			if (!lastLocation || !lastLocation.status || lastLocation.timestamp == 0 || lastLocation.lon == 0 || lastLocation.lat == 0) {
				return;
			}
			$api.setStorage("lon1", lastLocation.lon);
			$api.setStorage("lat1", lastLocation.lat);
				if(api.pageParam.online!=8){
					api.showProgress({
						title : '定位中……',
						modal : true
					});
				}
			window.setTimeout(function() {
				api.hideProgress();
				api.execScript({
					frameName : "upStationSign_frm",
					script : 'initRelief();'
				});
			}, 2000);
		}

		function closeWin() {
			api.closeWin({
				name : 'supportDefine_win'
			});
		}
	</script>
</html>
